// ==UserScript==
// @name         检测课程进度
// @namespace    http://tampermonkey.net/
// @version      2.0
// @description  try to take over the world!
// @author       You
// @match        https://www.zhifa315.com/index.php/ucenter/course_new
// @icon         https://www.zhifa315.com/favicon.ico
// @grant        GM_setValue
// @grant        GM_getValue
// https://gitee.com/clearlove123/video/raw/master/checkProgess.user.js
// ==/UserScript==
// 加一个获取学习进度百分比
(function () {
        var waitTime = 20 * 60 * 1000;
        // 获取用户名字
        const infoName = "info_" + getUserName();

        // 睡眠
        function sleep(time) {
            return new Promise((resolve) => setTimeout(resolve, time));
        }

        // 获取用户名字
        function getUserName() {
            return document.getElementsByClassName("exam-userinfo")[0].getElementsByTagName("span")[0].outerText;
        }

        // 获取当前时间

        function getTime(tt) {
            var hh = 0;
            var mm = 0;
            var ss = 0;
            // 24*60*60*1000
            var t = new Date(tt);
            if (tt < 86400000) {
                hh = parseInt(t / 1000 / 60 / 60);
                mm = parseInt(t / 1000 / 60);
                ss = parseInt(t % (1000 * 60) / 1000);
                t = new Date();
            }
            var h = t.getHours() + hh;
            var m = t.getMinutes() + mm;
            var s = t.getSeconds() + ss;
            if (s > 60) {
                s = s - 60;
                m++;
            }
            if (m > 60) {
                m = m - 60;
                h++;
            }
            if (h === 24) {
                h = 0
            }

            return h + "点" + m + "分" + s + "秒";
        }

        // 获取相差的时间时间
        function getGapTime(tt) {

            var t = new Date(tt);
            // 旧时间
            var hh = t.getHours();
            var mm = t.getMinutes();
            var ss = t.getSeconds();
            // 新时间
            var time = new Date();
            var h = time.getHours();
            var m = time.getMinutes();
            var s = time.getSeconds();
            if (s < ss) {
                m--;
                s = s + 60;
            }
            if (m < mm) {
                h--;
                m = m + 60;
            }
            return {
                "h": h - hh,
                "m": m - mm,
                "s": s - ss
            };
        }

        // 获取当前进度
        function getCourse() {
            let course = [];
            let ui = [];
            for (let i = 0; i <= 11; i++) {
                course[i] = document.getElementsByClassName("courseInfo")[i];
                ui[i] = course[i].getElementsByTagName("span")[1].outerText.slice(2, 5);
            }
            return ui;
        }

        // 打印日志
        function showLog(info) {
            console.log("---------------------");
            if (typeof (info) != "undefined") {
                console.log("名字：" + getUserName());
                console.log("上次刷新时间：" + getTime(info.time));
                var t = getGapTime(info.time);
                console.log("距离上次刷新时间：" + t.h + "小时" + t.m + "分钟" + t.s + "秒");
                console.log("将在：" + waitTime / 1000 / 60 + "分钟后刷新");
                console.log("下次刷新时间：" + getTime(waitTime));
                console.log("上次学了" + info.change + "%");
            }
            var log = localStorage.getItem("log");
            if (typeof (log) != "undefined") {
                log = log.split(",");
                console.log("正在学：" + log[0]);
                console.log("进度：" + log[1]);
                console.log("进度更新时间" + log[2]);
            }
        }

        // 关闭窗口
        async function closeWindows() {
            // 关闭其他窗口
            console.log("发送关闭指令...");
            // true的时候关闭
            localStorage.setItem("flag", true);
            await sleep(1000);
            let i = 0;
            // 检查窗口关闭没有
            while (true) {
                console.log("第" + i + "次等待关闭其他窗口...");
                if (i === 20) {
                    console.log("关闭其他窗口失败");
                    return;
                }
                if (localStorage.getItem("flag") === "false") {
                    console.log("第" + i + "次关闭其他窗口时成功...");
                    await sleep(1000);
                    return;
                }
                i++;
                // 间隔多少秒扫描一次
                await sleep(5 * 1000);
            }
        }

        // 打开新窗口
        async function openWindows(stuNum) {
            // 打开新的窗口
            var span = document.getElementsByClassName("courseInfo")[stuNum];
            console.log("正在打开新的窗口...");
            span.click();
        }

        async function test() {
            // 4秒+随机3到4秒
            var time = new Date();
            await sleep(1000);
            console.log("系统开始运行...");
            // 正在学习的序号
            var stuNum = 0;
            // 已学习时间
            var h = 0;
            // 正在学习的进度
            var newCourse;
            // 缓存里面的学习的进度
            var oldCourse;
            // 缓存里面的进度数组
            var progress;
            // 获取当前进度数组
            var course = getCourse();
            // 用名字从缓存拉取之前存的数据
            var info = GM_getValue(infoName);
            // 打印运行状态
            showLog(info);
            // 如果缓存没东西，就是首次打开页面
            if (typeof (info) == "undefined" || typeof (info.stuNum) == "undefined") {
                console.log("首次加载...");
                do {
                    newCourse = parseInt(course[stuNum++]);
                } while (newCourse === 100);
                stuNum--;
                info = {
                    "change": h,
                    'time': time,
                    'progress': course,
                    'stuNum': stuNum
                };
            }
            // 获取正在学习的序号
            stuNum = info.stuNum;
            // 获取缓存里面的进度数组
            progress = info.progress;
            newCourse = parseInt(course[stuNum]);
            oldCourse = parseInt(progress[stuNum]);
            h = newCourse - oldCourse;
            // 学完了
            if (newCourse === 100) {
                await closeWindows();
                await openWindows(++stuNum);
            }
            if (newCourse === 0) {
                console.log("刚开始学...");
            }
            // 间隔时间小于一定值，不打印
            var gapTime = getGapTime(time);
            if (info.change === 0 && h === 0) {
                if (gapTime.m > 5) {
                    console.log("---------------------");
                    console.log("学习失败...");
                    console.log("---------------------");
                }
            }

            // 写入缓存
            info = {
                "change": h,
                'time': Date.parse(time),
                'progress': course,
                'stuNum': stuNum
            };
            GM_setValue(infoName, info);
            console.log("本次刷新已经学习了" + h + "%");
            console.log("---------------------");

        }

        async function main() {
            test();
            console.log(getTime(new Date()));
            await sleep(waitTime);
            window.location.reload();
        }

        main();
    }

)
();